﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iShoppingBUS;
using iShoppingDTO;
using System.Web.Script.Serialization;

namespace iShopping.Guest.Actions
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string action = Request.Params["action"];
            if (action == "refreshVerificationCode") {
                string oldIdCode = Request.Params["idcode"];
                refreshVerificationCode(oldIdCode);
            }
            if (action == "register")
                register();
        }

        private void refreshVerificationCode(string oldIdCode) {
            MaXacNhanDTO mxn = MaXacNhanBUS.getMaXacNhanByMa(int.Parse(oldIdCode) + 1);
            if(mxn == null)
                mxn = MaXacNhanBUS.getMaXacNhanByMa(int.Parse(oldIdCode) - 1);
            JavaScriptSerializer ser = new JavaScriptSerializer();
            string res = ser.Serialize(mxn);
            Response.ContentType = "application/json";
            Response.Write(res);
            Response.End();
        }

        private void register() {

            if (TaiKhoanBUS.checkTenTaiKhoan(Request.Params["tenTaiKhoan"])) {
                Response.Write("Tên đăng nhập đã tồn tại");
                Response.End();
                return;
            }

            if (ThongTinTaiKhoanBUS.checkEmail(Request.Params["email"])) {
                Response.Write("Email này đã được sử dụng");
                Response.End();
                return;
            }

            if (!MaXacNhanBUS.checkMaXacNhan(int.Parse(Request.Params["idMaXacNhan"]), Request.Params["dapAn"])) {
                Response.Write("Mã xác kiểm tra không chính xác");
                Response.End();
                return;
            }

            DateTime date = DateTime.Now;
            long ticks = date.Ticks;
            TaiKhoanDTO tk = new TaiKhoanDTO();
            tk.MaTaiKhoan = ticks.ToString();
            tk.TenTaiKhoan = Request.Params["tenTaiKhoan"];
            tk.MatKhau = Request.Params["matKhau"];
            bool result = TaiKhoanBUS.insertTaiKhoan(tk);
            if (result)
            {
                ThongTinTaiKhoanDTO tttk = new ThongTinTaiKhoanDTO();
                tttk.MaTaiKhoan = ticks.ToString();
                tttk.HoTen = Request.Params["hoTen"];
                tttk.GioiTinh = Request.Params["gioiTinh"];
                tttk.MaTheTinDung = Request.Params["maTheTinDung"];
                tttk.NgaySinh = Request.Params["ngaySinh"];
                tttk.NoiSinh = Request.Params["noiSinh"];
                tttk.SoDienThoai = Request.Params["soDienThoai"];
                tttk.DiaChi = Request.Params["diaChi"];
                tttk.Email = Request.Params["email"];
                ThongTinTaiKhoanBUS.insertThongTinTK(tttk);
                Response.Write("success");
            }
            else {
                Response.Write("Đăng ký thất bại vui lòng thử lại lần sau");
            }
            Response.End();
        }
    }
}
